home *** CD-ROM | disk | FTP | other *** search
- Path: topaz.cqu.edu.au!naderr
- From: naderr@topaz.cqu.edu.au
- Newsgroups: comp.lang.c++,comp.lang.c
- Subject: Josephus Problem
- Date: 11 Apr 96 16:52:09 +1000
- Organization: Central Queensland University, Australia
- Message-ID: <1996Apr11.165209@topaz>
- NNTP-Posting-Host: topaz.cqu.edu.au
-
- Hi,
-
-
- Brief Description of Josephus Problem:
-
- The program reads two positive integers, n and q.
- Suppose n persons form a circle. In clockwise order, we assign
- numbers 1,2,...,n to them. Starting at person 1 and counting clockwise
- we remove the q-th person from the circles. In the reduced circle,
- we continue with the person following the person just removed and,
- resume counting from 1 to q, again eliminating the q-th person.
- This process is repeated until only one person remains: the program
- outputs the number n, of this person.
-
- I've recently coded this in C/C++ in two ways:
-
- (1) With objects modelling the problem and deriving n, by
- iterativley eliminating the q-th person. (it uses a double
- circular linked list)
-
-
- (2) According to the description given in:
-
-
- =============================================================================
- Functional Iteration and the Josephus Problem
- A. M. Odlyzko and H. S. Wilf
- Glasgow Math. J. 33 (1991), 235-240.
-
- Andrew Odlyzko
- Mathematical Sciences Research Center
- Room 2C-355
- AT&T Bell Laboratories
- 600 Mountain Avenue
- Murray Hill, NJ 07974, USA
- email: amo@research.att.com
- voice: 908-582-7286
- fax: 908-582-2379
-
- for WWW access, use the URL
- ftp://netlib.att.com/netlib/att/math/odlyzko/index.html.Z
-
- via ftp: connect to netlib.att.com, login as anonymous,
- give your e-mail address as password, and then do
- binary
- cd netlib/att/math/odlyzko
-
- =============================================================================
-
-
- And the algorithm I have used is as follows:
-
-
- The authors give the following procedure for finding J(n):
- q
-
-
- [A]
- _ _
- (q) | q (q) | (q)
- Define a sequence D = | ------- D | ( n >= 1; D = 1).
- n q - 1 n-1 0
- _ _
- | |
- where | | represents the 'ceiling' function.
-
-
- [B]
- (q)
- Determine the least integer k, such that D > (q - 1)n
- k
-
- [C]
- (q)
- Then the answer is J(n) = qn + 1 - D
- q k
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- Now,
-
- where my problem lies is in [B]:
-
- To search for k, I initialise k = 1 and increment it by 1 until the
- condition is satisfied. This is ok for small n,q; but for larger
- values ( say n = 10000, q = 700 ) it is obviusly not :))
-
- So, what I'm after is an algorithm that will minimise the iterations
- needed to determine k; a formula, anything :))
-
- Any suggestions, pointers, help are greatly appreciated.
-
- If replying to this message please do it to:
-
- naderr@topaz.cqu.edu.au
-
- If you want the C/C++ code for both the simple modelling, using elimination
- of nodes in the circle, and the mathematical functional iterative solution
- program that I coded I'd be happy to forward them to you.
-
-
- Thanks in advance,
-
- Rob -
-